package cn.nbt.busManage.dao.mapper;

import cn.nbt.busManage.model.dto.DriverDto;
import cn.nbt.busManage.model.dto.ScheduleDto;
import cn.nbt.busManage.model.entity.Driver;
import cn.nbt.busManage.model.entity.Schedule;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ScheduleMapper {
    /**
     * 根据信息查询司机
     *
     * @param query 查询信息（名称）
     * @return 司机信息
     */
//    @Select("select schedule.* from schedule,driver_schedule,driver where  driver.id=driver_schedule.Driver_id and driver_schedule.schedules_id=schedule.id and driver.driver_name like '%${query}%'")
    List<ScheduleDto> findByName(String query);

    /**
     * 根据司机信息查询上班表
     * @param driverId 查询信息（id）
     * @return 司机信息
     */
    @Select("select schedule.* from schedule,driver_schedule where  driver_schedule.schedules_id=schedule.id and driver.id = #{driverId}")
    List<Schedule> findByDriverId(Integer driverId);

    /**
     * 根据信息增加司机
     * @param schedule 增加信息
     */
    @Insert("insert into schedule(start_time,end_time) values(#{schedule.start_time},#{schedule.end_time}) select schedules_id from schedule,driver_schedule,driver where  driver.id=driver_schedule.Driver_id and driver_schedule.schedules_id=schedule.id and driver.id = #{driverId}")
    void save(Integer driverId,Schedule schedule);

    /**
     * 根据信息更新司机
     *
     * @param driverId 更新信息
     * @return 信息
     */
    @Update("update schedule set schedule.start_time=#{nschedule.start_time},schedule.end_time=#{nschedule.end_time} where schedule.id=(select schedules_id from schedule,driver_schedule,driver where  driver.id=driver_schedule.Driver_id and driver_schedule.schedules_id=schedule.id and driver.id = #{driverId})")
    List<Driver> update(Integer driverId, Schedule nschedule);

    /**
     * 根据信息删除司机
     * @param driverId 删除信息
     */
    @Delete("delete from driver_schedule where driver_schedule.Driver_id = #{driverId}")
    void deleteDriverSchedule(@Param("driverId") Integer driverId);

}
